﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using LowLevelGraphics;

using LowLevelGraphics.Extractors;
using NUnit.Framework;

namespace UnitTests
{
    /// <summary>
    /// This is used to test the cooccurrence matrix creation
    /// </summary>
    [TestFixture]
    public class CooccurrenceMatrixTest
    {
        protected UnsafeBitmap bitmap = new UnsafeBitmap(new Bitmap(6, 6));

        /// <summary>
        /// Sets up.
        /// </summary>
        [SetUp]
        public void SetUp()
        {
            //our testbitmap got the extensions 6,6
            bitmap = new UnsafeBitmap(new Bitmap(6, 6));
            Type[] aType = bitmap.GetType().Assembly.GetTypes();
            foreach (Type type in aType)
            {

            }
        }

        /// <summary>
        /// Tests the coocurrence matrix.
        /// </summary>
        [Test]
        public void TestCoocurrenceMatrix()
        {   
            //----------------------------------------------------
            //First test....

            //Test pic
            //int[,] aTestPic = new int[,]{
            //    {0, 0, 1, 1, 2, 3},
            //    {0, 0, 0, 1, 2, 3},
            //    {0, 0, 1, 2, 3, 3},
            //    {0, 1, 1, 2, 3, 3},
            //    {1, 2, 2, 3, 3, 3},
            //    {2, 2, 3, 3, 3, 3}
            //};

            //we should get this kernel from calculation
            //int[,] aKernel = new int[,] { 
            //    { 4, 4, 0, 0 },
            //    { 0, 2, 5, 6},
            //    { 0, 0, 2, 6},
            //    { 0, 0, 0, 7}
            //};

            //----------------------------------------------------
            //Second Test
            int[,] aTestPic = new int[,]{
                {0, 1, 0, 1, 2, 3},
                {0, 0, 1, 2, 2, 2},
                {0, 0, 1, 1, 1, 2},
                {0, 1, 2, 2, 3, 3},
                {0, 2, 2, 3, 3, 3},
                {2, 2, 2, 3, 3, 3}
            };

            int[,] aKernel = new int[,]{
                {2,5,1,0},
                {1,2,4,0},
                {0,0,6,4},
                {0,0,0,5}
            };

            FillBitmap(aTestPic);

            CoocurrenceMatrix m = new CoocurrenceMatrix();
            Assert.AreEqual(aKernel, m.Extract(bitmap));
            bitmap.UnlockImage();
        }

        [Test]
        public void Test2()
        {
            //Second Test
            int[,] aTestPic = new int[,]{
                {0, 1, 0, 1, 2, 3},
                {0, 0, 1, 2, 2, 2},
                {0, 0, 1, 1, 1, 2},
                {0, 1, 2, 2, 3, 3},
                {0, 2, 2, 3, 3, 3},
                {2, 2, 2, 3, 3, 3}
            };

            int[,] aKernel = new int[,]{
                {2,5,1,0},
                {1,2,4,0},
                {0,0,6,4},
                {0,0,0,5}
            };

            FillBitmap(aTestPic);

            CoocurrenceMatrix matrix = new CoocurrenceMatrix(new Point(1, 0));
            Assert.AreEqual(aKernel, matrix.Extract(bitmap));
            bitmap.UnlockImage();
        }

        /// <summary>
        /// Tears down.
        /// </summary>
        [TearDown]
        public void TearDown()
        {
        }

        /// <summary>
        /// Fills the bitmap. This is just a helper function for this test.
        /// </summary>
        /// <param name="aTestPic">A test pic.</param>
        private void FillBitmap(int[,] aTestPic)
        {
            for (int x = 0; x < 6; x++)
            {
                for (int y = 0; y < 6; y++)
                {
                    bitmap.SetGrayPixel(x, y, aTestPic[y, x]);
                }
            }
        }
    }
}

